package dp2.tablet.proyecto.util;

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import dp2.tablet.proyecto.Login;
import dp2.tablet.proyecto.abrircaja;

public class JSONRegistrarPago extends AsyncTask<String, Void, Void>{
	
	
	InputStream is = null;
	String result = "";
	JSONObject jArray = null;

 
    // constructor
    public JSONRegistrarPago() {
 
    }
 
    @Override
	protected Void doInBackground(String... url) {
		// Making HTTP request
    	
	    HttpParams myParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
	    HttpConnectionParams.setSoTimeout(myParams, 10000);
	    HttpClient httpclient = new DefaultHttpClient( myParams);
		
	    Double val_sol;
	    Double val_dol;
	    Double tarjeta;
	    
	    try{ val_sol = Double.parseDouble(url[1]); }
		catch(NumberFormatException e){ val_sol = 0.0; }
	    
	    try{ val_dol = Double.parseDouble(url[2]); }
		catch(NumberFormatException e){ val_dol = 0.0; }
	    
	    try{
	    	tarjeta = Double.parseDouble(url[3]);
	    }catch(NumberFormatException e){ tarjeta = 0.0; }
		
		  try {

		        HttpPost httppost = new HttpPost(url[0].toString());
		        httppost.setHeader("Content-type", "application/json");
		       
		        	JSONObject objeto = new JSONObject();	
		        	JSONObject inner = new JSONObject();
					
					
//					obj.put(TAG_ID, listaPromociones.getId());
		        	inner.put("id",null);
		        	inner.put("tipo_operacion","5");
		        	inner.put("monto_registrado", val_sol + val_dol);
		        	inner.put("cashier_id", abrircaja.getCaja().getId());
		        	inner.put("worker_id", Integer.valueOf(Login.getUsuario().getId()) );
		        	inner.put("created_at",null);
		        	inner.put("updated_at",null);
		        	inner.put("monto_soles", val_sol);
		        	inner.put("monto_dolares",val_dol);
		        	inner.put("monto_tarjeta",tarjeta);
					
		        	objeto.put("cashier_operation", inner);
					
		        StringEntity se = new StringEntity(objeto.toString()); 
		        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
		        httppost.setEntity(se); 

		        HttpResponse response = httpclient.execute(httppost);
		        String temp = EntityUtils.toString(response.getEntity());
		        Log.i("tag", temp);
		    

		    } catch (Exception e) {
		    	 Log.e("Buffer Error", "Error converting result " + e.toString());
			}
 
        // return JSON String
        return null;
	}
	
}
